package de.revdev.llt.module.classroom;

import de.revdev.llt.module.classroom.interfaces.ClassRoomInterface;
import de.revdev.llt.module.filecardboxes.FileCardBox;
import de.revdev.llt.module.filecardboxes.FileCardBoxFacade;
import de.revdev.llt.module.learning.LearnStatusFC;
import de.revdev.llt.module.learning.LearnStatusFCB;
import de.revdev.llt.module.learning.LearnStatusFCBFacade;
import de.revdev.llt.module.learning.LearnStatusFCFacade;
import de.revdev.llt.module.useradministrtaion.UserBean;
import java.io.Serializable;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.faces.model.ListDataModel;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author f.lindermaier
 */
@Named(value = "showClassRoomBean")
@SessionScoped
public class ShowClassRoomBean implements Serializable {

    private UserBean userBean;
    private LearnStatusFCBFacade lsfcbf;
    private LearnStatusFCFacade lsfcf;
    private FileCardBoxFacade fcbf;
    private PublicClassRoomFacade pucrf;
    private PrivateClassRoomFacade prcrf;
    private ClassRoomInterface classRoom;
    private LearnStatusFCB learnStatusFCB;
    private ListDataModel<FileCardBox> fileCardBoxDataModel;

    /**
     * Creates a new instance of ShowClassRoomBean
     */
    public ShowClassRoomBean() {
    }

    public String open(ClassRoomInterface classRoom) {
        this.classRoom = classRoom;
        fileCardBoxDataModel = new ListDataModel<>(classRoom.getFileCardBoxes());
        userBean.setClassRoomTemplate("/faces/user/showclassroom.xhtml");
        return "/faces/user/classroom.xhtml?faces-redirect=true";
    }

    public String open() {
        classRoom = userBean.getUser().getPrivateClassRoom();
        fileCardBoxDataModel = new ListDataModel<>(classRoom.getFileCardBoxes());
        userBean.setClassRoomTemplate("/faces/user/showclassroom.xhtml");
        return "/faces/user/classroom.xhtml?faces-redirect=true";
    }

    public boolean roleAdmin() {
        if (classRoom instanceof PublicClassRoom) {
            PublicClassRoom pucr = (PublicClassRoom) getClassRoom();
            for (Participants p : pucr.getParticipants()) {
                if (p.getUser().equals(userBean.getUser()) && p.getClassRoomRole().equals(Participants.ClassRoomRole.ADMIN)) {
                    return true;
                }
            }
            return pucr.getOwner().equals(userBean.getUser());
        } else {
            return false;
        }
    }

    public boolean roleEditor() {
        for (Participants p : ((PublicClassRoom) classRoom).getParticipants()) {
            if (p.getUser().equals(userBean.getUser())
                    && (p.getClassRoomRole().equals(Participants.ClassRoomRole.ADMIN)
                    || p.getClassRoomRole().equals(Participants.ClassRoomRole.WRITER))) {
                return true;
            }
        }
        return ((PublicClassRoom) classRoom).getOwner().equals(userBean.getUser());
    }

    public List<Participants> participantsForClassRoom() {
        if (classRoom instanceof PublicClassRoom) {
            return ((PublicClassRoom) classRoom).getParticipants();
        } else {
            return null;
        }
    }

    public String typeString() {
        if (getClassRoom() instanceof PublicClassRoom) {
            return "public";
        } else {
            return "private";
        }
    }

    public boolean listFileCardBoxIsEmpty() {
        return classRoom.getFileCardBoxes().isEmpty();
    }

    public int getUnknownLearnStatus() {
        learnStatusFCB = lsfcbf.getLearnStatusForFileCardBox(userBean.getUser(), fileCardBoxDataModel.getRowData());
        if (learnStatusFCB == null) {
            return fileCardBoxDataModel.getRowData().getFileCards().size();
        } else {
            return fileCardBoxDataModel.getRowData().getFileCards().size() - learnStatusFCB.getLearnStatusFC().size();
        }
    }

    public long getWorstStatusForFileCards() {
        if (learnStatusFCB == null) {
            return 0;
        } else {
            return lsfcf.countStatusFromFCB(LearnStatusFC.Status.WORST, learnStatusFCB);
        }
    }

    public long getBadStatusForFileCards() {
        if (learnStatusFCB == null) {
            return 0;
        } else {
            return lsfcf.countStatusFromFCB(LearnStatusFC.Status.BAD, learnStatusFCB);
        }
    }

    public long getGoodStatusForFileCards() {
        if (learnStatusFCB == null) {
            return 0;
        } else {
            return lsfcf.countStatusFromFCB(LearnStatusFC.Status.GOOD, learnStatusFCB);
        }
    }

    public long getBetterStatusForFileCards() {
        if (learnStatusFCB == null) {
            return 0;
        } else {
            return lsfcf.countStatusFromFCB(LearnStatusFC.Status.BETTER, learnStatusFCB);
        }
    }

    public long getBestStatusForFileCards() {
        if (learnStatusFCB == null) {
            return 0;
        } else {
            return lsfcf.countStatusFromFCB(LearnStatusFC.Status.BEST, learnStatusFCB);
        }
    }

    @Inject
    public void setPublicClassRoomFacade(PublicClassRoomFacade publicClassRoomFacade) {
        this.pucrf = publicClassRoomFacade;
    }

    @Inject
    public void setPrivateClassRoomFacade(PrivateClassRoomFacade privateClassRoomFacade) {
        this.prcrf = privateClassRoomFacade;
    }

    @Inject
    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    @Inject
    public void setLearnStatusFCBFacade(LearnStatusFCBFacade learnStatusFCBFacade) {
        this.lsfcbf = learnStatusFCBFacade;
    }

    @Inject
    public void setLearnStatusFCFacade(LearnStatusFCFacade learnStatusFCFacade) {
        this.lsfcf = learnStatusFCFacade;
    }

    @Inject
    public void setFileCardBosFacade(FileCardBoxFacade fileCardBoxFacade) {
        this.fcbf = fileCardBoxFacade;
    }

    public ClassRoomInterface getClassRoom() {
        return classRoom;
    }

    public void setClassRoom(ClassRoomInterface classRoom) {
        this.classRoom = classRoom;
    }

    public LearnStatusFCB getLearnStatusFCB() {
        return learnStatusFCB;
    }

    public void setLearnStatusFCB(LearnStatusFCB learnStatusFCB) {
        this.learnStatusFCB = learnStatusFCB;
    }

    public ListDataModel<FileCardBox> getFileCardBoxDataModel() {
        return fileCardBoxDataModel;
    }

    public void setFileCardBoxDataModel(ListDataModel<FileCardBox> fileCardBoxDataModel) {
        this.fileCardBoxDataModel = fileCardBoxDataModel;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
 
}